package com.syh.jn.scm.service.impl.supplier.base;

import cn.redscarf.commons.util.other.ExtAssert;
import cn.redscarf.commons.util.validator.ValidationUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import com.syh.jn.scm.domain.supplier.entity.base.SalesTeam;
import com.syh.jn.scm.domain.supplier.repository.base.SalesTeamRepository;
import com.syh.jn.scm.dto.supplier.base.salesTeam.SalesTeamMto;
import com.syh.jn.scm.dto.supplier.base.salesTeam.SalesTeamSto;
import com.syh.jn.scm.dto.supplier.base.salesTeam.SalesTeamVo;
import com.syh.jn.scm.service.api.supplier.base.SalesTeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * 销售团队管理
 */
@Service
@Transactional
public class SalesTeamServiceImpl extends AbstractServiceImpl<SalesTeamSto, SalesTeamMto, SalesTeamVo, SalesTeam, Long> implements SalesTeamService {
  @Autowired
  private SalesTeamRepository salesTeamRepository;

  @Override
  protected AggregateRepository<SalesTeam, Long> getAggregateRepository() {
    return salesTeamRepository;
  }

  /**
   * 启用销售团队
   *
   * @param id
   */
  @Override
  public void enabled(Long id) {
    //根据id获取销售团队信息并且判空
    SalesTeam salesTeam = this.getSalesTeamById(id);

    salesTeam.enabled();
    salesTeamRepository.update(salesTeam);
  }

  /**
   * 停用销售团队
   *
   * @param id
   */
  @Override
  public void disabled(Long id) {
    //根据id获取销售团队信息并且判空
    SalesTeam salesTeam = this.getSalesTeamById(id);

    salesTeam.disabled();
    salesTeamRepository.update(salesTeam);
  }

  /**
   * 保存对象
   *
   * @param sto
   * @return
   */
  @Override
  public Long save(SalesTeamSto sto) {
    ExtAssert.notNull(sto, "对象为空");
    ValidationUtils.validate(sto);

    SalesTeam salesTeam = SalesTeam.createdSalesTeamBySupply(sto);
    return salesTeamRepository.add(salesTeam);
  }

  /**
   * 修改对象
   *
   * @param mto
   */
  @Override
  public void update(SalesTeamMto mto) {
    ExtAssert.notNull(mto, "对象为空");
    ValidationUtils.validate(mto);

    //根据id获取销售团队信息并且判空
    SalesTeam salesTeam = this.getSalesTeamById(mto.getId());

    salesTeam.modify(mto);
    salesTeamRepository.update(salesTeam);
  }

  /**
   * 删除对象
   *
   * @param id
   */
  @Override
  public void delete(Long id) {
    //根据id获取销售团队信息并且判空
    SalesTeam salesTeam = this.getSalesTeamById(id);

    salesTeam.destroy();
    salesTeamRepository.removeById(id);
  }

  /**
   * 根据id获取销售团队信息并且判空
   *
   * @param id
   * @return
   */
  private SalesTeam getSalesTeamById(Long id) {
    ExtAssert.notNull(id, "id为空");

    SalesTeam salesTeam = salesTeamRepository.getById(id);
    ExtAssert.notNull(salesTeam, "对象为空");

    return salesTeam;
  }
}
